package com.settlement.system.controller;

import com.settlement.system.csv.CsvExportBuilder;
import com.settlement.system.csv.CsvExportUtil;
import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblWpThisMonthIosSummary;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpBrandUnitConfigService;
import com.settlement.system.service.ITblWpThisMonthIosSummaryService;
import com.settlement.system.service.csv.DateSpiltCsvDatas;
import com.settlement.system.common.util.common.FasUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * AP_543-本月收发存数量汇总表 前端控制器
 * @author Ximi
 * @since 2020-10-28
 */

@Slf4j
@Api(tags = "AP_543-本月收发存数量汇总表",description = "AP_543-本月收发存数量汇总表")
@RestController
@RequestMapping("/wp/tblWpThisMonthIosSummary")
public class TblWpThisMonthIosSummaryController extends MergeApiExcelController<TblWpThisMonthIosSummary, Map<String, Object>> {

    @Value("${system.export-root.path}")
    private String exportRootPath;

    @Resource
    private ITblWpThisMonthIosSummaryService tblWpThisMonthIosSummaryService;

    @Resource
    private ITblWpBrandUnitConfigService tblWpBrandUnitConfigService;

    @Override
    public IBaseService init() {
        return tblWpThisMonthIosSummaryService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblWpThisMonthIosSummary.class;
    }


    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        TblWpThisMonthIosSummary foot=tblWpThisMonthIosSummaryService.getFoot(model);
        result.put("foot", foot==null? getModelClass():foot);
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model, "vendorCode", "shopNo", "poNo", "barcode","orderSchedule","thisPeriodEndQtyType");
        super.setParamsModel(model);
    }

    @Override
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> params = buildParam(request);
        setParamsModel(params);
        Object fileName = "WP-本月收发存数量汇总";
        String path = exportRootPath + System.currentTimeMillis() + "/";
        CsvExportUtil.createCsv()
                .headClass(getModelClass())
                .fileName(fileName.toString())
                .model(CsvExportBuilder.MULTI_ZIP_MODEL)
                .parentPath(path)
                .csvDatas(new DateSpiltCsvDatas(tblWpThisMonthIosSummaryService, params))
                .export(request,response);
    }

    /**
     * 本月收发存汇总-刷新字段
     *
     * @return
     */
    @ApiOperation(value = "本月收发存汇总-刷新字段")
    @RequestMapping(value = "/api/refreshField", method = RequestMethod.POST)
    public ObjectDataResponse refreshField() {
        try {
            tblWpThisMonthIosSummaryService.refreshField();
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_SUCCESS.getValue(), "success");
        } catch (Exception ex) {
            log.error("本月收发存汇总-刷新字段出错", ex);
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL.getValue(), "服务器异常");
        }
    }
}

